%addpath(genpath('/afs/inf.ed.ac.uk/user/s07/s0785186/Desktop/Project'));

global width;               % width of the playing field
global height;              % height of the playing field
global num_players_r;       % number of players on the red team
global num_players_b;       % number of players on the blue team
global ball_position;       % initial position of the ball

global epsilon;             % constant for epsilon-greedy selection
global gamma;               % discount rate
global lambda;              % eligibility trace value
global alpha;               % step-size constant
global num_tilings;         % number of overlapping layers of tiles

global opponent;            % opponent parameters
global fixed_strategy;      % fixed strategy for team blue
global learning_algorithm;  % learning algorith in case system is in learning mode
global policy_file;         % .m file containing the policy to be followed

global starting_policy;	    % the starting policy on which the learning agents will build
global starting_point;	    % indicates how many learning episodes were completed while obtaining the starting policy

global compare_policies;    % boolean value indicating whether multiple policies should be compared
global policy_names;        % the names of the policies being compared

global record_states;       % boolean value indicating whether the states should be recorded in state_set
global state_set;           % an array containing the recorded states

global system_mode;         % the running mode of the system
global home_path;           % the path leading to the root directory of the project
global keyword;             % a string appended to the end of a policy file 

home_path = '/home/georgi/Desktop/Project/';
%home_path = '/afs/inf.ed.ac.uk/user/s07/s0785186/Desktop/Project/';

% a list of parametrized opponents
op1 = struct('name','OP1','pass',0.3, 'dribble', 0.7, 'get_open', 0.4, 'block', 0.1, 'go_to_ball', 0.5, 'pace', 10, 'defensive_line', 20, 'offensive_line', -40);
op2 = struct('name','OP2','pass',0.2, 'dribble', 0.8, 'get_open', 0.1, 'block', 0.2, 'go_to_ball', 0.7, 'pace', 10, 'defensive_line', 40, 'offensive_line', -40);
op3 = struct('name','OP3','pass',0.5, 'dribble', 0.5, 'get_open', 0.4, 'block', 0.1, 'go_to_ball', 0.5, 'pace', 10, 'defensive_line', 40, 'offensive_line', -40);
op4 = struct('name','OP4','pass',0.1, 'dribble', 0.9, 'get_open', 0.2, 'block', 0.2, 'go_to_ball', 0.6, 'pace', 10, 'defensive_line', 40, 'offensive_line', -40);
op5 = struct('name','OP5','pass',0.0, 'dribble', 1.0, 'get_open', 0.0, 'block', 0.0, 'go_to_ball', 1.0, 'pace', 10, 'defensive_line', 40, 'offensive_line', -60);

heu = struct('name','OP_HEU','pass',0.0, 'dribble', 1.0, 'get_open', 0.3, 'block', 0.3, 'go_to_ball', 0.6, 'pace', 10, 'defensive_line', 40, 'offensive_line', 60);
op_def = struct('name','OP_DEF','pace',10);
op6 = struct('name','OP6','pass',0.7, 'dribble', 0.3, 'get_open', 0.6, 'block', 0.1, 'go_to_ball', 0.3, 'pace', 10, 'defensive_line', 20, 'offensive_line', -20);

test_op = struct('name','test_op','pass',0.15, 'dribble', 0.85, 'get_open', 0.05, 'block', 0.5, 'go_to_ball', 0.45, 'pace', 10, 'defensive_line', 30, 'offensive_line', -40);

indices = [10,30,50,100,150,200,250,300,400,500,600,800,1000,1200,1400,1600,1800, 2000,2250,2500,2750,3000, 3250,3500, 3750, 4000, 4500, 5000];

% set game parameters
width = 120;
height = 60;
num_players_r = 4;
num_players_b = 4;
ball_position = struct('B_X',rand_num(-20,20),'B_Y',rand_num(-20,20));

% set learning algorithm parameters
epsilon = 0.1;              % original = 0.1;
gamma = 0.9;                % original = 0.9;
lambda = 0.5;               % original = 0.5;
num_tilings = 32;           % original = 32;   
alpha = 1/(10*num_tilings); % original 1/(10*num_tilings);

% set the learning algorithm
learning_algorithm = @Sarsa;

% % change to HeuristicSelect
fixed_strategy = @HeuristicSelect;
%fixed_strategy = @GoKick;

% recordinf states or comparing policies?
record_states = false;
compare_policies = false;
policy_names = {'policy_12500_OP1', 'policy_12500_OP4'};

%------------------------------------------------------------------------%
% learn a policy
%opponent = struct('name','OP2','pass',0.2, 'dribble', 0.8, 'get_open', 0.1, 'block', 0.2, 'go_to_ball', 0.7, 'pace', 5, 'defensive_line', 40, 'offensive_line', -40);

% system_mode = 'load';
% opponent = op2;
% keyword = strcat('OP2_TEST');
%  
% starting_point = 5000;
% starting_policy = strcat('policy_',num2str(starting_point),'_',keyword);
%  
% start 0 10000

%------------------------------------------------------------------------%
% test a policy
% for i = 1:numel(indices),
%     opponent = op4;
%     keyword = 'OP4_TEST';
%     system_mode = 'follow';
%     policy_file = strcat('policy_',num2str(indices(i)),'_',keyword);
%     start 0 100
% end

% for i = 1:numel(indices),
%     opponent = op3;
%     keyword = 'OP3_TEST';
%     system_mode = 'follow';
%     policy_file = strcat('policy_',num2str(indices(i)),'_',keyword);
%     start 0 100
% end

% width = 80;
% height = 50;
% 
% for i = 1:numel(indices),
%     opponent = op2;
%     keyword = 'OP2_TEST_SMALL';
%     system_mode = 'follow';
%     policy_file = strcat('policy_',num2str(indices(i)),'_',keyword);
%     start 0 100
% end


%------------------------------------------------------------------------%
    opponent = op2;
    keyword = 'OP2_TEST';
    system_mode = 'follow';
    policy_file = strcat('policy_',num2str(4000),'_',keyword);
    start 0 1

%------------------------------------------------------------------------%
% sending email when job completes
%!pwd | mail -s finished g.gochev@gmail.com
%------------------------------------------------------------------------%
